<?php

#[用户第三方绑定记录管理]
class MemberoauthController extends AcommonController
{
    public function init() {
        $this->model = (new memberoauthModel());
        $this->table = 'member_oauth';
        $this->primaryKey = 'id';
        parent::init();
    }

    #[用户第三方绑定列表]
    public function indexAction($returnData = FALSE)
    {
        if($this->isPost()){
            $this->model = $this->model->with(['member' => function ($memberQuery){
                $memberQuery->select('id', 'nickname', 'realname', 'phone', 'avatar', 'status', 'deleted_at');
            }]);
            $post_data = $this->postData;
            $search_data = [];
            if(isset($post_data['mode']) && !empty($post_data['mode']) && $post_data['mode'] != 'all'){
                array_push($search_data, ['platform', '=', $post_data['mode']]);
            }
            if(isset($post_data['memberoauth_start']) && !empty($post_data['memberoauth_start'])){
                $this->model = $this->model->whereDate('created_at', '>=', $post_data['memberoauth_start']);
            }
            if(isset($post_data['memberoauth_end']) && !empty($post_data['memberoauth_end'])){
                $this->model = $this->model->whereDate('created_at', '<=', $post_data['memberoauth_end']);
            }
            $keywordsTypeArray = [
                'user_id' => 'user_id',
                'user_phone' => 'user_phone',
            ];
            if(isset($post_data['keywordsType']) && isset($post_data['keywords'])
                && in_array($post_data['keywordsType'], $keywordsTypeArray)
                && !empty($post_data['keywords'])){
                if($post_data['keywordsType'] == $keywordsTypeArray['user_id'] && is_numeric($post_data['keywords']) && intval($post_data['keywords']) > 0){
                    array_push($search_data, ['member_id', '=', $post_data['keywords']]);
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['user_phone']){
                    if(mb_strlen($post_data['keywords'], 'utf-8') < 4){
                        array_push($search_data, ['id', '=', 0]);
                    }else{
                        array_push($this->condition_in, ['member_id', memberModel::query()->where('phone', 'like', "%{$post_data['keywords']}%")->pluck('id')->toArray()]);
                    }
                }
            }
            unset($keywordsTypeArray);
            $this->search = $search_data;
            $this->appends = ['platform_text', 'logintime_text'];
            return parent::indexAction($returnData);

        }else{
            return parent::indexAction($returnData);
        }

    }

    #[解除用户第三方绑定]
    public function unbindAction()
    {
        if(!$this->isPost()){
            ret(3, '请求出错');
        }
        $id = $this->get('id');
        if(!is_numeric($id) || $id <= 0){
            ret(4, '参数出错');
        }
        $oauth = $this->model->where('id', $id)->first();
        if(!$oauth){
            ret(5, '无绑定记录');
        }
        $oauth_array = $oauth->toArray();
        $remark = $this->get('remark', '管理员解除绑定');
        $member_oauth_log = [
            'event' => 'unbind',
            'event_id' => $oauth->id,
            'remark' => $remark,
            'oper_type' => 'admin',
            'oper_id' => $this->auth->user_id,
            'created_at' => timestamp()
        ];
        unset($oauth_array['id'], $oauth_array['created_at'], $oauth_array['updated_at']);
        $member_oauth_log = array_merge($member_oauth_log, $oauth_array);
        try {
            if(memberoauthModel::query()->where('id', $id)->delete()){
                \app\modules\Admin\models\memberoauthlogModel::query()->insert($member_oauth_log);
            }
            ret(0, '解除绑定成功！');
        }catch (Exception $e){
            ret(1, $e->getMessage());
        }
    }
}